package com.lazily.controller;


import com.lazily.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 测试请求参数接收
 */

@RestController
public class RequestController {

	// // 原始方式
	// @RequestMapping("/simpleParam")
	// public String simpleParam(HttpServletRequest request) {
	// 	// 获取请求参数
	// 	String name = request.getParameter("name");
	// 	String ageStr = request.getParameter("age");
	//
	// 	int age = Integer.parseInt(ageStr);
	// 	System.out.println(name + " " + age);
	// 	return "OK";
	// }


	// springboot方式
	// @RequestMapping("/simpleParam")
	// public String simpleParam(String name, Integer age) {
	// 	System.out.println(name + " " + age);
	// 	return "OK";
	// }


	// @RequestParam注解的使用
	@RequestMapping("/simpleParam")
	// @RequestParam(name = "name") 指定请求参数的key, 使用了此注解默认必须传递该参数, 否则报错, 可以自己设置为不传递
	public String simpleParam(@RequestParam(name = "name", required = false) String username, Integer age) {
		System.out.println(username + " " + age);
		return "OK";
	}

	// 2. 实体参数
	@RequestMapping("/simplePojo")
	public String simplePojo(User user) {
		System.out.println(user);
		return "OK";
	}

	@RequestMapping("/complexPojo")
	public String complexPojo(User user) {
		System.out.println(user);
		return "OK";
	}

	// 3. 数组集合参数
	@RequestMapping("/arrayParam")
	public String arrayParam(String[] hobby) {
		System.out.println(Arrays.toString(hobby));
		return "OK";
	}
}
